//
//  HMVisitorView.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/8/30.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
/// 定义协议
@objc protocol HMVisitorViewDelegate : NSObjectProtocol{
    func visitorViewWillLogin()
    @objc optional func visitorViewWillRegister()
}

class HMVisitorView: UIView {
    
    
    // MARK: - 懒加载控件
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 初始化视图
    private func setupUI() {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 1. 添加控件
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2. 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        /**
         - 要约束的对象
         - 要约束的属性 left,right
         - 等于
         - 相对于谁来约束
         - 对方控件的约束属性
         - 乘积
         - 偏移量
         
         */
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        
        // 使用 snapkit/用法基本与 Masonry 类型
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        // 消息label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        
        // 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        // 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        maskIconView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
    }
    
    // MARK: - 外部调用的方法
    
    // 提供一个方法，供外界设置访客视图里面的信息
    func setVisitorViewInfo(imgName: String?, message: String?) {
        
        if imgName != nil {
            // 如果传入的图片名字有值，就代表是消息/发现/我这三个页面
            // 因为首页不用传图片名字
            circleView.isHidden = true
            iconView.image = UIImage(named: imgName!)
            messageLabel.text = message
        }else{
            messageLabel.text = message
            // 在这个地方让圆圈转起来
            startAnimation()
        }
    }
    
    // 开启动画
    func startAnimation() {
        // 动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置到某个值：一圈
        anim.toValue = M_PI * 2
        // 重复次数
        anim.repeatCount = MAXFLOAT
        // 设置执行一次动画需要的时间
        anim.duration = 20
        // 是否移除在完成的时候。如果切换界面或者应用程序退到后台，那么动画会被释放掉。指定这个值的话就代表在做以上两个操作的时候不让其移除
        anim.isRemovedOnCompletion = false
        // 将动画添加到layer上
        circleView.layer.add(anim, forKey: nil)
    }
    
    // MARK: - 懒加载控件
    
    // 房子的图标
    private lazy var iconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    // 转圈
    private lazy var circleView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    // 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 14)
        label.text = "哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        // 多行
        label.numberOfLines = 0
        // 居中
        label.textAlignment = .center
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orange, fontSize: 14)
        button.setTitle("注册", for: UIControlState.normal)
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: UIControlState.normal)
        return button;
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGray, fontSize: 14)
        button.setTitle("登录", for: UIControlState.normal)
        button.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: UIControlState.normal)
        return button;
    }()
    
    // 半透明视图
    private lazy var maskIconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
}
